package com.xzz.finger;


/**
 * @author: hhz
 * @create: 2022-02-21 11:07
 * 剑指 Offer 22. 链表中倒数第k个节点
 **/
public class GetKthFromEnd {
    public static void main(String[] args) {
        ListNode node = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6, new ListNode(7)))))));
        System.out.println(getKthFromEnd(node, 4));

    }

    public static ListNode getKthFromEnd(ListNode head, int k) {
        ListNode fast = head;
        ListNode last = head;
        for (int i = 0; i < k; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            last = last.next;
        }
        return last;
    }

}
